import {
  getProfileAPI,
  getUserInfoAPI,
  loginAPI,
  sendCodeAPI,
  updateProfileAPI,
} from "@/api/user";
import history from "@/utils/history";
import { saveAuth } from "@/utils/storage";
import { Toast } from "antd-mobile";
import { AppDispatch } from "..";
import { uploadPhotoAPI } from "@/api/user";

export const sendCodeAction = (mobile: string) => {
  return async () => {
    await sendCodeAPI(mobile);
  };
};

export const loginAction = (mobile: string, code: string) => {
  return async () => {
    const res = await loginAPI(mobile, code);
    Toast.show("登录成功");
    saveAuth(res.data);
    history.push("/");
  };
};

export const getUserInfoAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getUserInfoAPI();
    dispatch({ type: "user/saveUserInfo", payload: res.data });
  };
};

export const getProfileAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getProfileAPI();
    dispatch({ type: "user/saveProfile", payload: res.data });
  };
};

export const updatePhotoAction = (formData: FormData) => {
  return async () => {
    await uploadPhotoAPI(formData);
  };
};

// 2/
export const updateProfileActon = (profile: Partial<IProfile>) => {
  return async () => {
    const res = await updateProfileAPI(profile);
    console.log("res  ----->  ", res);
  };
};
